const {ipcRenderer} = require('electron');
var io = require('./io');
var audioplayer;

RequestOrderDetail();

/** 
 * 获取指定的URL参数值 
 * URL:http://www.quwan.com/index?name=tyler 
 * 参数：paramName URL参数 
 * 调用方法:getParam("name") 
 * 返回值:tyler 
 */ 
function getParam(paramName) { 
    paramValue = "", isFound = !1; 
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { 
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; 
        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ 
    } 
    return paramValue == "" && (paramValue = null), paramValue 
} 

function RequestOrderDetail(){
    var orderid = getParam("orderid");
    var user = getParam("user");
    io.websocketemit('orderdetail', {user:user ,orderid:orderid});
}

var OrderDetail;

io.onwebsocket('orderdetail', (data)=>{
    if(data == null){
        document.getElementById("divContent").innerHTML = "查询不到该订单！！！";
        return;
    }
    OrderDetail = data;
    console.log(data);
    SetupTop(data);
    SetupFiles(data);
});

function SetupTop(data){
    var stateClass = "待处理";    
    if(data.state == 1){
        stateClass = "已完成";              
    }
    var btnFinishText = "<button class='btnFinish' style='float:left'>完成</button>";
    if(data.state == 1){
        btnFinishText = "";
    }
    var buttons = btnFinishText+"<button class='audio-order-download' style='float:right'>开始下载</button>";

    document.getElementById("orderid").innerText = '订单编号:{0}'.format(data.orderid);
    document.getElementById("desc").innerText = '描述:{0}'.format(data.desc);
    document.getElementById("endtime").innerText = '截止时间:{0}'.format(data.endtime);
    document.getElementById("filecount").innerText = '文件数量:{0}'.format(data.infos.length);
    document.getElementById("state").innerHTML = '状态:{0}'.format(stateClass);
    document.getElementById("buttons").innerHTML = buttons;

    $("button.btnFinish").bind("click", function(){
        io.websocketemit('finishorder', {user:OrderDetail.user ,orderid:OrderDetail.orderid});
    });

    $("button.audio-order-download").bind("click", function(){
        ipcRenderer.send('downloadall',{baseurl:io.geturl(),files:OrderDetail.infos});
    });
}

function SetupFiles(data){
    //0=id,1=name,2=downnum,3=tips,4=duration,5=quality
    var itemTemplate = "\
    <div style='width:49%;float:left;margin:0.5%'>\
        <ul class='audio-item-box-row' >\
        <li class='audio-item-box audio-item-box-search-mode-false'>\
            <div class='pkg-item-chk-div'></div>\
            <div class='audio-item-row audio-item-row1'>\
                <div class='title'>\
                    <span class='title-name' title='{1}' style='margin-left:15px;margin-right:5px;'>\
                        {1}\
                    </span>\
                </div>\
            </div>\
            <div class='audio-item-row audio-item-row2'>\
                <div style='width:550px;height:30px'>\
                    <div style='width:50px;float:left;text-align:center;margin-top:25px;'><span id='play.{5}' key='{5}' class='glyphicon glyphicon-play btn-play'></span></div>\
                    <div id='waveform-{5}' style='width:500px;float:left;'></div>\
                </div>\
                <div class='unit-box-from prevent-default-event'>\
                    {3}\
                </div>\
            </div>\
        </li>\
        </ul >\
    </div>";
    
    var htmls = "";
    for (var i = 0; i < data.infos.length; i++) {
        var item = data.infos[i];

        var tagsHtml = "";
        if(data.tags != undefined && data.tags != null){//not test.
            for(var t = 0; t < data.tags.length; t++){
                tagsHtml += data.tags[t] + (t < data.tags.length - 1 ? "/" : "");
            }
        }
        htmls += itemTemplate.format(item.id, item.name, item.suffix, tagsHtml, io.geturl(), i);
    }
    var content = document.getElementById("divContent");
    content.innerHTML = htmls;   

    audioplayer = new Array(data.infos.length);
    for (var i = 0; i < data.infos.length; i++) {
        audioplayer[i] = WaveSurfer.create({
            container: '#waveform-' + i,
            waveColor: 'violet',
            progressColor: 'purple',
            height: 70,
        });
        var item = data.infos[i];
        var url = '{0}{1}{2}'.format(io.geturl(),item.id,item.suffix);
        audioplayer[i].load(url);
    }

    $(".btn-play").bind("click", function () {
        let key = $(this).attr('key');
        audioplayer[key].play();
    });
}

io.onwebsocket('finishorder', (data)=>{
    if(data == 1){
        //通知order页面刷新
                
        //io.websocketemit('allorder', null);
    }
});
